import mysql.connector
import pandas as pd
from pyspark.sql import SparkSession

appName = "PySpark MySQL Example - via mysql.connector"
master = "local"

spark = SparkSession.builder.master(master).appName(appName).getOrCreate()

# Establish a connection
conn = mysql.connector.connect(user='root', database='jjj',
                               password='s3cret',
                               host="172.16.0.59",
                               port=3306)
#cursor = conn.cursor()
query = "SELECT menu_id, title FROM jjjshop_center_menu"
# Create a pandas dataframe
pdf = pd.read_sql(query, con=conn)
conn.close()

# Convert Pandas dataframe to spark DataFrame
df = spark.createDataFrame(pdf)

df.show()

spark.stop()
